package com.zelin.page.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zelin.dao.GoodsDao;
import com.zelin.dao.GoodsDescDao;
import com.zelin.dao.ItemCatDao;
import com.zelin.dao.ItemDao;
import com.zelin.entity.GoodsDescEntity;
import com.zelin.entity.GoodsEntity;
import com.zelin.entity.ItemEntity;
import com.zelin.page.service.PageSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import org.thymeleaf.context.IContext;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/8/6-14:37
 * ------------------------------
 */
@Service
public class PageServiceImpl implements PageSerivce {

    //1. 引入模板引擎
    @Autowired
    private TemplateEngine engine;
    //2. 引入其它mapper
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private GoodsDescDao goodsDescDao;
    @Autowired
    private ItemDao itemDao;
    @Autowired
    private ItemCatDao itemCatDao;

    private static final String FILE_PATH = "D:\\nginx-1.8.0\\html\\item\\";
    /**
     * 功能: 根据spu商品id生成静态页面
     * 参数:
     * 返回值: void
     * 时间: 2021/8/6 14:37
     */
    @Override
    public void createHtml(Long goodsId) throws IOException {
        //1. 定义模板工作的上下文环境对象
        Context context = new Context();
        //2. 定义存放商品数据的集合
        Map<String, Object> goodsMap = new HashMap<>();

        //4. 向集合中添加内容
        //4.1 查询spu商品信息
        GoodsEntity goodsEntity = goodsDao.selectById(goodsId);

        //4.1.1 查询商品分类名称
        String category1 = itemCatDao.selectById(goodsEntity.getCategory1Id()).getName();  //一级分类
        String category2 = itemCatDao.selectById(goodsEntity.getCategory2Id()).getName();  //二级分类
        String category3 = itemCatDao.selectById(goodsEntity.getCategory3Id()).getName();  //三级分类

        //4.2 查询商品描述信息
        GoodsDescEntity goodsDescEntity = goodsDescDao.selectById(goodsId);
        //4.3 查询sku商品列表
        QueryWrapper<ItemEntity> itemQueryWrapper = new QueryWrapper<ItemEntity>().eq("goods_id", goodsId)
                                                                .orderByDesc("is_default");
        List<ItemEntity> items = itemDao.selectList(itemQueryWrapper);
        //4.4 处理ItemEntity中的spec字符串为Map对象
        for (ItemEntity item : items) {
            String spec = item.getSpec();
            Map map = JSON.parseObject(spec, Map.class);
            item.setSpecMap(map);
        }
        //4.5 重新组织数据
        List<Map> itemImages = JSON.parseArray(goodsDescEntity.getItemImages(), Map.class);
        List<Map> customAttributeItems = JSON.parseArray(goodsDescEntity.getCustomAttributeItems(), Map.class);
        List<Map> specificationItems = JSON.parseArray(goodsDescEntity.getSpecificationItems(), Map.class);

        //将商品信息放到map中
        goodsMap.put("goods",goodsEntity);
        goodsMap.put("itemImages",itemImages);
        goodsMap.put("customAttributeItems",customAttributeItems);
        goodsMap.put("specificationItems",specificationItems);
        goodsMap.put("goodsDesc",goodsDescEntity);
        goodsMap.put("items",items);
        goodsMap.put("category1",category1);
        goodsMap.put("category2",category2);
        goodsMap.put("category3",category3);

        //3. 绑定集合
        context.setVariables(goodsMap);

        //定义要输出的文件流
        File file = new File(FILE_PATH + goodsId + ".html");
        Writer writer = new FileWriter(file);

        //执行相关操作
        engine.process("item",context,writer);
    }

    /**
     * 功能: 根据商品id删除静态页面
     * 参数:
     * 返回值: void
     * 时间: 2021/8/9 15:51
     */
    @Override
    public void deletePage(List<Long> ids) {
        for (Long id : ids) {
            String path =   FILE_PATH + id + ".html";
            System.out.println("path = " + path);
            new File(path).delete();
        }
    }
}
